<?php
/**
 * Created by PhpStorm.
 * User: Qch
 * Date: 2016/10/17
 * Time: 15:59
 */

namespace Application\Admin\Controller;


use J\Intent;
use J\MVC\Controller;
use J\Util\Node;
use J\Util\Tree;

class IndexController extends Controller
{
    protected function beforeCore()
    {
        parent::beforeCore();

        $this->enableAdmin(self::AUTH_LOGIN);

        $this->addMiddleware(function (\Closure $next){
            return function (Intent $intent) use ($next){
                $r = $next($intent);
                // 意图没有被处理则跳转到对应的模块中去
                if($r === false) return $intent->jump($intent->action.'/Admin/Index');
                return $r;
            };
        });
    }

    protected function optionIndex()
    {

    }

    protected function optionElementUIIndex(Intent $intent)
    {
        $intent->value = [
            'view'=>[
                'name'=>'Admin',
                'url' => '/api.php/' . $intent->getPathinfo('Params')
            ]
        ];
    }

    protected function optionApiParams(Intent $intent)
    {
        $intent->value = [
            'title' => 'JPHP-VUE管理后台',
            'menus' => $this->getMenus(),
            'user'  => [
                'name' => $this->getApp()->User()->nickname,
                'commands' => $this->getCommands()
            ],
        ];
    }

    protected function optionApiReLoad(Intent $intent)
    {
        // app()->currentUser()->setReload(true);
        $menus = $this->getMenus();
        // app()->currentUser()->setReload(false);

        $intent->value = [
            'title' => 'JPHP-VUE管理后台',
            'menus' => $menus,
            // 'user'  => $this->user->nickname,
            'commands' => $this->getCommands()
        ];
    }

    protected function getMenus()
    {
        try {
            $tree = new Tree();
            $user = $this->getApp()->User();
            $rows = $tree->fromList($user->authorize, 'resource')->getRoot()->map(function (Node $node) {
                $row = $node->getValue();
                $row['label'] = $row['title'];
                $row['index'] = $row['id'];
                $file = ($row['file']) ? $row['file'] : '/element.php';
                $row['url'] = $file . $row['url'];
                return $row;
            },'items');
            // 添加超级管理员菜单
            return $this->administratorMenus(array_isset($rows,'items',[]));
        }catch(\Exception $e){
            return [];
        }
    }

    /**
     * 超级管理员特有功能
     * @param array $menus
     * @return array
     */
    protected function administratorMenus($menus)
    {
        if($this->getModule()->getApp()->User()->isAdministrator()){
            $menus[] = [
                'index' => 'system',
                'label' => '系统管理',
                'items' => [
                    [
                        'index' => 'DatabaseConfig',
                        'label' => '数据库配置',
                        'url'=> '/element.php/Config/Database/index'
                    ],
                    [
                        'index' => 'CacheConfig',
                        'label' => '缓存配置',
                        'url'=> '/element.php/Config/Cache/index'
                    ],
                    [
                        'index' => 'AdminManage',
                        'label' => '用户中心安装',
                        'confirm' => '该操作将删除现有的用户中心数据',
                        'url' => '/element.php/UserCenter/Install/Index'
                    ]
                ]
            ];
            $menus[] = [
                'index' => 'tools',
                'label' => '常用工具',
                'items' => [
                    [
                        'index' => 'json',
                        'label' => 'Json转换',
                        'url' => '/element.php/index/index/json'
                    ],
                ]
            ];
            $menus[] = [
                'index' => 'module',
                'label' => '模块管理',
                'items' => [
                    [
                        'index' => 'ModuleInstall',
                        'label' => '模块安装',
                        'confirm' => '该操作可能会删除现有的模块数据',
                        'url' => '/element.php/Admin/Super/Install'
                    ],
                    [
                        'index' => 'ModuleUninstall',
                        'label' => '模块卸载',
                        'confirm' => '请慎重',
                        'url' => '/element.php/Admin/Super/Uninstall'
                    ],
                    [
                        'index' => 'ModuleDesign',
                        'label' => '模块设计',
                        'url' => '/element.php/Admin/Module/Index'
                    ],
                ]
            ];
        }
        return $menus;
    }

    protected function getCommands()
    {
        return [
            [
                'index' => 'reMenu',
                'label' =>'刷新信息',
                'url'   => '/api.php/Admin/Index/reLoad'
            ], [
                'index' => 'logout',
                'label' => '退出系统',
                'url' => '/element.php/userCenter/Login/exit'
            ]
        ];
    }

    protected function getParam()
    {
        return [
            'title' => 'JPHP-VUE管理后台',
            'menus' => $this->getMenus(),
            'user'  => [
                'name' => $this->getApp()->User()->nickname,
                'commands' => $this->getCommands()
            ],
        ];
    }
}